<?php
if ($act != "" && $detail == "" || $detail == "all") {
    $match_timestamp = Frontend::findMatchByTimeStamp((int) $_today)->count();
} else {
    $tour_url = Frontend::findTournamentByUrl($detail);
    foreach ($tour_url as $_tour_url) {
        $match_timestamp = Frontend::findMatchByTourIdTimeStamp((int)$_tour_url["_id"], (int) $_today)->count();
    }
}
if ($page == NULL) {
    $_page = 1;
} else {
    $_page = $page;
}

$numPage = ceil($match_timestamp / $num_page);
$showeachside = 5;
$eitherside = ($showeachside * 1);

if (empty($page))
    $start = 0;
else
    $start = $page;

$pageCurrent = $num_page * ($_page - 1);

if ($act != "" && $detail == "" || $detail == "all") {
    $url = "";
    $match_all = Frontend::findMatchByTimeStamp((int) $_today)->skip($pageCurrent)->limit($num_page);
} else {
    $url = $detail . "/";
    $match_all = Frontend::findMatchByTourIdTimeStamp((int)$_tour_url["_id"], (int) $_today)->skip($pageCurrent)->limit($num_page);
}
$arr_match = array();
$arr = iterator_to_array($match_all);

foreach ($match_all as $keys => $_match_all) {
    $datematch = explode(" ", $_match_all["DateMatch"]);
    $arr_match[$datematch[0]][] = $arr[$keys];
}
//echo "<pre>";
//var_export(iterator_to_array($match_all)); 
?>
<div id="main" class="row">
    <div class="span14">
        <div class="span3">
            <ul class="nav nav-list nav-list-vivid">
                <li class="nav-header">Giải đấu</li>
                <li><a href="<?php echo SKIN_FPATH . "dudoanbongda/all" ?>">Tất cả</a></li>
                <?php
                $country = Frontend::findAllCountry();
                foreach ($country as $_country) {
                    ?>
                    <li class="nav-header"><?php echo $_country["name"] ?></li>
                    <?php
                    $tournament = Frontend::findTournamentByCountry((int)$_country["_id"]);
                    foreach ($tournament as $_tournament) {
                        $str = trim(Core::str_process($_tournament["title"]));
                        $link = strtolower(preg_replace('/[^a-z0-9]+/i', '-', $str));
                        
                        $c_match = Frontend::findMatchByTourIdTimeStamp((int)$_tournament["_id"], (int) $_today);
                        ?>
                        <li class="children">
                            <a href="<?php echo SKIN_FPATH . "dudoanbongda/" . $link; ?>">
                                <img src="/skin/frontend/caulacbo/oYVHSYPZZ4.png" class="logo" width="20">
                                <?php echo $_tournament["title"]; ?>                
                                <span class="label label-inverse pull-right"><?php echo count(iterator_to_array($c_match)); ?></span>
                            </a>
                        </li>
                    <?php } ?>
                <?php } ?>
            </ul>
        </div>

        <div class="span9">
            <div class="headnav clearfix" style="margin-bottom: 18px;">
                <div class="btn-group" style="float: left">
                    <a class="btn active">Lựa chọn ngày</a>
                    <a href="#" class="active btn btn-warning dropdown-toggle" data-toggle="dropdown">
                        <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu dropdown-special">
                        <?php
                        for ($j = 0; $j < 7; $j++) :
                            $day = strtotime(date("Y-m-d", strtotime($today)) . " +{$j} day");
                            $d = strtotime(date("Y-m-d", strtotime($today)) . " +{$j} day");
                            if ($j == 0) {
                                $d = strftime("%d-%m-%Y", $day);
                                $day = "Hôm nay, " . strftime("%d-%m", $day);
                            } else {
                                $d = strftime("%d-%m-%Y", $day);
                                $day = strftime("%A, %d-%m", $day);
                            }
                            ?>
                            <li><a href="<?php echo SKIN_FPATH . "dudoanbongda/" . $d ?>"><?php echo $day ?></a></li>
                        <?php endfor; ?>
                    </ul>
                </div>
                <div class="btn-group pull-right">
                    <?php if ($page > 1) { ?>
                        <a class="btn" href="<?php echo SKIN_FPATH . "dudoanbongda/" . $url . 1 ?>"><< First</a>
                        <a class="btn" href="<?php echo SKIN_FPATH . "dudoanbongda/" . $url . ($_page - 1) ?>">Prev</a>
                    <?php } else { ?>
                        <a class="btn disabled" href="javascript:void(0)"><< First</a>
                        <a class="btn disabled" href="javascript:void(0)">Prev</a>
                    <?php } ?>    
                    <?php
                    for ($i = 1; $i <= $numPage; $i++) {
                        if (($i > ($start - $eitherside)) && ($i < ($start + $eitherside))) {
                            ?>
                            <a class="btn <?php
                            if ($i == $_page) {
                                echo "active";
                            }
                            ?>" href="<?php echo SKIN_FPATH . "dudoanbongda/" . $url . $i ?>"><?php echo $i ?></a>
                               <?php
                           }
                       }
                       ?>
                       <?php if ($_page < $numPage) { ?>
                        <a class="btn" href="<?php echo SKIN_FPATH . "dudoanbongda/" . $url . ($_page + 1) ?>">Next</a>
                        <a class="btn" href="<?php echo SKIN_FPATH . "dudoanbongda/" . $url . $numPage ?>">Last >></a>
                    <?php } else { ?>
                        <a class="btn disabled" href="javascript:void(0)">Next</a>
                        <a class="btn disabled" href="javascript:void(0)">Last >></a>
                    <?php } ?>
                </div>
            </div>

            <table class="table table-bordered table-events">
                <?php
                foreach ($arr_match as $key => $_arr_match) :
                    $date = date('l, Y-m-d', strtotime($key));
                    ?>
                    <thead>
                        <tr>
                            <th colspan="10">
                                <img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/Flaticon_4430.png"/>
                                <?php echo $date; ?>
                            </th>
                        </tr>
                    </thead>

                    <tbody>
                        <tr class="head">
                            <td rowspan="2" class="time">Giờ</td>
                            <td class="p" rowspan="2" colspan="2">Trận</td>
                            <td colspan="3">1X2 Toàn Trận</td>
                            <td rowspan="2">Chấp Toàn Trận</td>
                            <td rowspan="2">Trên / Dưới</td>
                            <td rowspan="2"></td>
                        </tr>
                        <tr class="head">
                            <td>1</td>
                            <td>X</td>
                            <td>2</td>
                        </tr>
                        <?php
                        // Tìm Match theo ngày
                        foreach ($_arr_match as $_match_date) :
                            $team1 = Frontend::findTeamById((int) $_match_date["Team1"]);
                            foreach ($team1 as $_team1) :
                                $t1 = $_team1["name"];
                            endforeach;
                            $team2 = Frontend::findTeamById((int) $_match_date["Team2"]);
                            foreach ($team2 as $_team2) :
                                $t2 = $_team2["name"];
                            endforeach;
                            
                            $str_team = trim(Core::str_process($t1." ".$t2));
                            $link_team = strtolower(preg_replace('/[^a-z0-9]+/i', '-', $str_team));
                            $match_id = $_match_date["_id"];
                                    
                            $odd = Frontend::findOddByMacth((int)$_match_date["_id"]);
                            $link_match = Frontend::findTournamentById($_match_date["TourID"]);
                            //if (iterator_to_array($odd)):
                            ?>
                            <tr class="odd">
                                <td rowspan="2" class="time">
                                    <img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/Flaticon_4076.png"/><br>
                                    <?php echo $_match_date["StartTime"] ?>
                                </td>
                                <td class="p" colspan="2">
                                    <?php
                                        echo $t1;
                                    ?>  
                                </td>
                                <?php
                                if (iterator_to_array($odd)) {
                                    foreach ($odd as $_odd):
                                        ?>
                                        <!--------------- 1X2 Toàn Trận  --------------------------->
                                        <?php
                                        if ($_odd["typeID"] == "ODD_FT") :
                                            $value = explode("|", $_odd["value"]);

                                            if ($value[0] != "" && $value[0] != "chưa có tỷ lệ") {
                                                foreach ($value as $key => $_value):
                                                    $data = array('id' => $_odd['_id'], 'value' => $_value, 'time' => $_match_date['DateMatchTS'], 'index_key' => $key);
                                                    $encode = json_encode($data);
                                                    $base_encode = base64_encode($encode);
                                                    ?>
                                                    <?php if ($key == 0) { ?>
                                                        <td rowspan="2" class="odds">    
                                                            <a href="javascript:void(0)" id="<?php echo $_odd["_id"] ?>" class="add-to-betslip" data_value='<?php print_r($base_encode) ?>' onclick="addodd('<?php echo $base_encode; ?>');"><?php echo $_value ?></a>
                                                        </td>
                                                    <?php } elseif ($key == 1) { ?>
                                                        <td rowspan="2" class="odds decrease">    
                                                            <a href="javascript:void(0)" id="<?php echo $_odd["_id"] ?>" class="add-to-betslip" data_value='<?php print_r($base_encode) ?>' onclick="addodd('<?php echo $base_encode; ?>');"><?php echo $_value ?></a>
                                                        </td>
                                                    <?php } elseif ($key == 2) { ?>
                                                        <td rowspan="2" class="odds increase">    
                                                            <a href="javascript:void(0)" id="<?php echo $_odd["_id"] ?>" class="add-to-betslip" data_value='<?php print_r($base_encode) ?>' onclick="addodd('<?php echo $base_encode; ?>');"><?php echo $_value ?></a>
                                                        </td>
                                                    <?php } ?>
                                                <?php endforeach; ?>
                                            <?php } else { ?>
                                                <td rowspan="2" colspan="3" class="odds increase">    
                                                    Chưa có tỷ lệ.
                                                </td>    
                                            <?php } ?>
                                        <?php endif; ?>
                                        <!--------------- Kết Thúc 1X2 Toàn Trận  --------------------------->
                                        <!--------------- Chấp Toàn Trận  --------------------------->
                                        <?php if ($_odd["typeID"] == "ODD_FTHDP") : ?>

                                            <td style="text-align: right;" class="odds odds-large">
                                                <?php
                                                $value = explode("|", $_odd["value"]);
                                                foreach ($value as $key => $_value):
                                                    $data = array('id' => $_odd['_id'], 'value' => $_value, 'time' => $_match_date['DateMatchTS']);
                                                    $encode = json_encode($data);
                                                    $base_encode = base64_encode($encode);
                                                    ?>
                                                    <?php if ($key == 0) { ?>
                                                        <span class="pull-left label label-warning"><?php echo $_value ?></span> 
                                                    <?php } elseif ($key == 1) { ?>
                                                        <a href="javascript:void(0)" id="<?php echo $_odd["_id"] ?>" class="add-to-betslip" data_value='<?php print_r($base_encode) ?>' onclick="addodd('<?php echo $base_encode; ?>');"><?php echo $_value ?></a>
                                                    <?php } ?>
                                                <?php endforeach; ?>
                                            </td>
                                        <?php endif; ?>
                                        <!--------------- Kết Thúc Chấp Toàn Trận  --------------------------->
                                        <!--------------- Trên / Dưới  --------------------------->
                                        <?php if ($_odd["typeID"] == "ODD_UO") : ?>
                                            <td style="text-align: right;" class="odds odds-large">    
                                                <?php
                                                $value = explode("|", $_odd["value"]);
                                                foreach ($value as $key => $_value):
                                                    $data = array('id' => $_odd['_id'], 'value' => $_value, 'time' => $_match_date['DateMatchTS']);
                                                    $encode = json_encode($data);
                                                    $base_encode = base64_encode($encode);
                                                    ?>
                                                    <?php if ($key == 0) { ?>
                                                        <span class="pull-left label label-warning"><?php echo $value[0] ?></span> 
                                                    <?php } elseif ($key == 1) { ?>
                                                        <a href="javascript:void(0)" id="<?php echo $_odd["_id"] ?>" class="add-to-betslip" data_value='<?php print_r($base_encode) ?>' onclick="addodd('<?php echo $base_encode; ?>');"><?php echo $value[1] ?></a>
                                                    <?php } ?>
                                                <?php endforeach; ?>
                                            </td>
                                        <?php endif; ?>
                                        <!--------------- Kết Thúc Trên / Dưới  --------------------------->
                                    <?php endforeach; ?>
                                <?php } else { ?>
                                    <td rowspan="2" colspan="3" class="odds increase">    
                                        Chưa có tỷ lệ.
                                    </td>  
                                    <td rowspan="2" style="text-align: left;" class="odds odds-large"> 
                                        Chưa có tỷ lệ.
                                    </td>
                                    <td rowspan="2" style="text-align: left;" class="odds odds-large"> 
                                        Chưa có tỷ lệ.
                                    </td>
                                <?php } ?>
                                <td rowspan="2" class="go">
                                    <?php foreach ($link_match as $_link_match) { ?>
                                        <a href="<?php echo SKIN_FPATH . "dudoanbongda/" . $_link_match["location"] . "/" . $link_team . "/" . $match_id ?>">
                                            <img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/Flaticon_1413.png" />
                                        </a>
                                    <?php } ?>
                                </td>
                            </tr>
                            <tr class="odd">
                                <td class="p" colspan="2">
                                    <?php
                                        echo $t2;
                                    ?>  
                                </td>
                                <?php
                                foreach ($odd as $_odd):
                                    ?>
                                    <!--------------- Chấp Toàn Trận  --------------------------->
                                    <?php if ($_odd["typeID"] == "ODD_FTHDP") : ?>
                                        <?php
                                        $value = explode("|", $_odd["value"]);
                                        foreach ($value as $key => $_value):
                                            $data = array('id' => $_odd['_id'], 'value' => $_value, 'time' => $_match_date['DateMatchTS']);
                                            $encode = json_encode($data);
                                            $base_encode = base64_encode($encode);
                                            ?>
                                            <?php if ($key == 2) { ?>
                                                <td style="text-align: right;" class="odds odds-large">    
                                                    <a href="javascript:void(0)" id="<?php echo $_odd["_id"] ?>" class="add-to-betslip" data_value='<?php print_r($base_encode) ?>' onclick="addodd('<?php echo $base_encode; ?>');"><?php echo $_value ?></a>
                                                </td>
                                            <?php } ?>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                    <!--------------- Kết Thúc Chấp Toàn Trận  --------------------------->
                                    <!--------------- Trên / Dưới  --------------------------->
                                    <?php if ($_odd["typeID"] == "ODD_UO") : ?>
                                        <?php
                                        $value = explode("|", $_odd["value"]);
                                        foreach ($value as $key => $_value):
                                            $data = array('id' => $_odd['_id'], 'value' => $_value, 'time' => $_match_date['DateMatchTS']);
                                            $encode = json_encode($data);
                                            $base_encode = base64_encode($encode);
                                            ?>
                                            <?php if ($key == 2) { ?>
                                                <td style="text-align: right;" class="odds odds-large">    
                                                    <a href="javascript:void(0)" id="<?php echo $_odd["_id"] ?>" class="add-to-betslip" data_value='<?php print_r($base_encode) ?>' onclick="addodd('<?php echo $base_encode; ?>');"><?php echo $_value ?></a>
                                                </td>
                                            <?php } ?>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                    <!--------------- Kết Thúc Trên / Dưới  --------------------------->
                                <?php endforeach; ?>
                            </tr>
                            <?php //endif;  ?>
                        <?php endforeach; ?>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <div class="clearfix" style="margin-top: 18px;">
            </div>
            <div class="btn-group pull-right">
                    <!--<a class="btn" href="#"><?php //echo $match_timestamp     ?> kết quả</a>-->
                <?php if ($page > 1) { ?>
                    <a class="btn" href="<?php echo SKIN_FPATH . "dudoanbongda/" . 1 ?>"><< First</a>
                    <a class="btn" href="<?php echo SKIN_FPATH . "dudoanbongda/" . ($_page - 1) ?>">Prev</a>
                <?php } else { ?>
                    <a class="btn disabled" href="javascript:void(0)"><< First</a>
                    <a class="btn disabled" href="javascript:void(0)">Prev</a>
                <?php } ?>
                <?php
                for ($i = 1; $i <= $numPage; $i++) {
                    if (($i > ($start - $eitherside)) && ($i < ($start + $eitherside))) {
                        ?>
                        <a class="btn <?php
                if ($i == $_page) {
                    echo "active";
                }
                        ?>" href="<?php echo SKIN_FPATH . "dudoanbongda/" . $i ?>"><?php echo $i ?></a>
                           <?php
                       }
                   }
                   ?>
                   <?php if ($_page < $numPage) { ?>
                    <a class="btn" href="<?php echo SKIN_FPATH . "dudoanbongda/" . ($_page + 1) ?>">Next</a>
                    <a class="btn" href="<?php echo SKIN_FPATH . "dudoanbongda/" . $numPage ?>">Last >></a>
                <?php } else { ?>
                    <a class="btn disabled" href="javascript:void(0)">Next</a>
                    <a class="btn disabled" href="javascript:void(0)">Last >></a>
                <?php } ?>
            </div>
        </div>
    </div>
</div>